﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TimServerSDK.Core;
using TimServerSDK.Services.Portraits.Dtos;

namespace TimServerSDK.Portraits
{
    public class PortraitTimServerApi : AbstractTimServerApi, IPortraitTimServerApi
    {
        protected override string ServiceName { get; } = "profile";

        public PortraitTimServerApi(IRestClientProvider restClientProvider) : base(restClientProvider)
        {

        }

        public Task<TimServerResult> SetAsync(string fromAccount, Dictionary<string, string> profileItem)
        {
            return PostAsync<TimServerResult>("portrait_set", new
            {
                From_Account = fromAccount,
                ProfileItem = profileItem.Select(item =>
                {
                    return new
                    {
                        Tag = item.Key,
                        Value = item.Value
                    };
                }).ToArray()
            });
        }

        public Task<TimServerResult> SetAsync(string fromAccount, string nick = null, string faceUrl = null)
        {
            var profileItem = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(nick))
            {
                profileItem.Add("Tag_Profile_IM_Nick", nick);
            }
            if (!string.IsNullOrEmpty(faceUrl))
            {
                profileItem.Add("Tag_Profile_IM_Image", faceUrl);
            }
            return SetAsync(fromAccount, profileItem);
        }

        public Task<GetPortraitResultDto> GetAsync(string[] toAccount, string[] tagList)
        {
            return PostAsync<GetPortraitResultDto>("portrait_get", new
            {
                To_Account = toAccount,
                TagList = tagList
            });
        }
    }
}
